package com.cloudkinto.controller.order.input;


import cn.hutool.core.util.StrUtil;
import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.order.inbound.*;
import com.cloudkinto.bo.product.NewProductQueryBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.box.OrderBoxService;
import com.cloudkinto.service.order.input.OrderInputService;
import com.cloudkinto.service.product.ProductService;
import com.cloudkinto.vo.common.CommonBatchOperationVo;
import com.cloudkinto.vo.order.inbound.OrderInputDetailVo;
import com.cloudkinto.vo.order.inbound.OrderScanResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * Wms到货通知单前端控制器
 *
 * @author long
 * @since 2024-11-04
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "到货通知单（订单入库）模块")
@RequestMapping("/api/admin/wms/order/input")
public class OrderInputWmsController extends BaseController {
    private final OrderInputService orderInputService;
    private final ProductService productService;
    private final OrderBoxService boxService;

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看入库单详情", notes = "根据id查看入库单详情")
    @ApiResponses(@ApiResponse(code = 200, message = "请求成功", response = OrderInputDetailVo.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", orderInputService.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/scan")
    @ApiOperation(value = "到仓扫描", notes = "到仓扫描")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = OrderScanResultVo.class))
    public SingleResult<List<OrderScanResultVo>> scan(@RequestParam("condition") String condition) {
        if (StrUtil.isBlank(condition)) {
            throw new BizException(SysConstant.Query_Condition_Empty);
        }
        return SingleResult.success("查询成功", orderInputService.scan(condition, getWarehouseId(), getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/acceptGoodsDetailInit")
    @ApiOperation(value = "收货页初始化", notes = "收货页初始化")
    public SingleResult acceptGoodsDetailInit() {
        return orderInputService.acceptGoodsDetailInit();
    }

    @UserSessionRequired
    @PostMapping("/acceptGoodsDetail")
    @ApiOperation(value = "根据id查看收货页订单详情", notes = "根据id查看收货页订单详情")
    public SingleResult acceptGoodsDetail(@RequestBody @Validated AcceptGoodsDetailQueryBo bo) {
        return SingleResult.success("查询成功", orderInputService.acceptGoodsDetail(bo));
    }

    @UserSessionRequired
    @PostMapping("/acceptGoodsListPage")
    @ApiOperation(value = "根据id查看收货页产品详情", notes = "根据id查看收货页产品详情")
    public PageResult acceptGoodsListPage(@RequestBody @Validated AcceptGoodsQueryBo bo) {
        return orderInputService.acceptGoodsListPage(bo);
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "到货通知单（订单入库）查询初始化", notes = "到货通知单（订单入库）查询初始化")
    public SingleResult pageInit() {
        return orderInputService.pageInit(getCompanyId(), getTenantId(), "wms", getCurrentUserId());
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "到货通知单（订单入库）查询（分页）", notes = "到货通知单（订单入库）查询（分页）")
    public PageResult listPage(@RequestBody @Validated OrderInputQueryBo bo) {
        bo.setWhId(getWarehouseId());
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        return orderInputService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/listPageStatusStatistics")
    @ApiOperation(value = "分页查询状态数量统计", notes = "分页查询状态数量统计")
    public SingleResult listPageStatus(@RequestBody @Validated OrderInputQueryBo bo) {
        bo.setWhId(getWarehouseId());
        bo.setTenantId(getTenantId());
        bo.setStatus(null);
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        return orderInputService.getListPageStatus(bo);
    }

    @UserSessionRequired
    @PutMapping("/updateExpectedDate")
    @ApiOperation(value = "修改预计到货时间", notes = "修改预计到货时间")
    public SingleResult updateExpectedDate(@RequestParam("id") Long id, @RequestParam("expectedDate") String expectedDate) {
        orderInputService.updateExpectedDate(id, expectedDate, getCurrentUserId(), getUserName());
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @PutMapping("/receiptTotal/{id}")
    @ApiOperation(value = "整单收货", notes = "整单收货")
    public SingleResult receiptTotal(@PathVariable("id") Long id) {
        return SingleResult.success("整单收货完成", orderInputService.receiptTotalById(id, getCurrentUserId(), getUserName()));
    }

    @UserSessionRequired
    @PutMapping("/forceEnd/{id}")
    @ApiOperation(value = "强制完结", notes = "强制完结")
    public SingleResult forceEnd(@PathVariable("id") Long id) {
        orderInputService.forceEnd(id, getCurrentUserId(), getUserName());
        return SingleResult.success();
    }

    @UserSessionRequired
    @PostMapping("/normal/receiptConfirm")
    @ApiOperation(value = "常规入库收货确认按钮", notes = "常规入库收货确认按钮")
    public SingleResult normalReceiptConfirm(@RequestBody @Validated NormalReceiptConfirmBo bo) {
        return SingleResult.success(
                "收货确认成功",
                orderInputService.normalReceiptConfirm(bo, getCurrentUserId(), getUserName())
        );
    }

    @UserSessionRequired
    @PostMapping("/transfer/receiptConfirm")
    @ApiOperation(value = "备货中转收货确认按钮", notes = "备货中转收货确认按钮")
    public SingleResult receiptConfirm(@RequestBody @Validated TransferReceiptConfirmBo bo) {
        return SingleResult.success(
                "收货确认成功",
                orderInputService.transferReceiptConfirm(bo, getCurrentUserId(), getUserName())
        );
    }

    @UserSessionRequired
    @PutMapping("/updatePalletQty")
    @ApiOperation(value = "修改托盘数", notes = "修改托盘数")
    public SingleResult updatePalletQty(@RequestParam("id") Long id, @RequestParam("palletQty") Integer palletQty) {
        orderInputService.updatePalletQty(id, palletQty, getCurrentUserId(), getUserName());
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @PostMapping("/newProductKeepPageList")
    @ApiOperation(value = "新品维护分页", notes = "新品维护分页")
    public PageResult newProductKeepPageList(@RequestBody NewProductQueryBo bo) {
        Long tenantId = getTenantId();
        return productService.getNewProductKeepPageList(bo, tenantId);
    }

    @UserSessionRequired
    @PostMapping("/normal/receiptConfirmNewProductKeepList")
    @ApiOperation(value = "常规入库收货确认前新品维护查询", notes = "常规入库收货确认前新品维护查询")
    public SingleResult normalReceiptConfirmNewProductKeepList(@RequestBody @Validated NormalReceiptConfirmBo bo) {
        Long tenantId = getTenantId();
        return SingleResult.success(productService.normalReceiptConfirmNewProductKeepList(bo, tenantId));
    }

    @UserSessionRequired
    @PutMapping("/normal/receiptConfirmNewProductKeep")
    @ApiOperation(value = "常规入库收货确认前新品维护", notes = "常规入库收货确认前新品维护")
    public SingleResult normalReceiptConfirmNewProductKeep(@RequestBody @NotNull List<NormalReceiptConfirmNewProductBo> bo) {
        Long tenantId = getTenantId();
        productService.normalReceiptConfirmNewProductKeep(bo, getCurrentUserId(), getUserName(), tenantId);
        return SingleResult.success("新品维护提交成功");
    }

    @UserSessionRequired
    @PostMapping("/transfer/receiptConfirmNewProductKeepList")
    @ApiOperation(value = "中转入库收货确认前新品维护查询", notes = "中转入库收货确认前新品维护查询")
    public SingleResult transferReceiptConfirmNewProductKeepList(@RequestBody @Validated TransferReceiptConfirmBo bo) {
        return SingleResult.success(boxService.transferReceiptConfirmNewProductKeepList(bo));
    }

    @UserSessionRequired
    @PutMapping("/transfer/receiptConfirmNewProductKeep")
    @ApiOperation(value = "中转入库收货确认前新品维护", notes = "中转入库收货确认前新品维护")
    public SingleResult transferReceiptConfirmNewProductKeep(@RequestBody @NotNull List<TransferReceiptConfirmNewProductBo> bo) {
        boxService.transferReceiptConfirmNewProductKeep(bo);
        return SingleResult.success("新品维护提交成功");
    }

    @UserSessionRequired
    @PostMapping("/printSkuCodeLabelPdf")
    @ApiOperation(value = "打印SKU标签", notes = "打印SKU标签")
    public void printSkuCodePdf(@RequestBody SkuCodePrintPdfBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        orderInputService.exportSkuLabel(bo, response);
    }

    @UserSessionRequired
    @PostMapping("/printPackageLabelPdf")
    @ApiOperation(value = "打印箱唛标签", notes = "打印箱唛标签")
    public void printPackageLabelPdf(@RequestBody PackageLabelPrintPdfBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        orderInputService.printPackageLabelPdf(bo, response);
    }

    @UserSessionRequired
    @PostMapping("/excelExport")
    @ApiOperation(value = "导出到货通知单", notes = "导出到货通知单")
    public void excelExport(@RequestBody OrderInputQueryBo bo, HttpServletResponse response) throws IOException {
        bo.setTenantId(getTenantId());
        bo.setUserId(getCurrentUserId());
        bo.setSource("wms");
        orderInputService.excelExport(bo, getTenantId(), getCompanyId(), getWarehouseId(), response);
    }

    @UserSessionRequired
    @GetMapping("/printReceivingNote/{id}")
    @ApiOperation(value = "打印收货单", notes = "打印收货单")
    public void printReceivingNote(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        orderInputService.printReceivingNote(id, response);
    }

    @UserSessionRequired
    @GetMapping("/printReceivingNoteBoxBarcode/{id}")
    @ApiOperation(value = "打印收货单(箱条码)", notes = "打印收货单(箱条码)")
    public void printReceivingNoteBoxBarcode(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        orderInputService.printReceivingNoteBoxBarcode(id, response);
    }

    @UserSessionRequired
    @GetMapping("/printReceivingNoteSku/{id}")
    @ApiOperation(value = "打印收货单(SKU)", notes = "打印收货单(SKU)")
    public void printReceivingNoteSku(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        orderInputService.printReceivingNoteSku(id, getWarehouseId(), response);
    }

    @UserSessionRequired
    @PostMapping("/postToJdyInputOrder")
    @ApiOperation(value = "调拨发货计划推送到筋斗云", notes = "调拨发货计划推送到筋斗云")
    public SingleResult<CommonBatchOperationVo> postToJdyInputOrder(@RequestBody @Validated InboundPostToJdyBo bo) {
        return orderInputService.postToJdyInputOrder(bo);
    }
}

